<?php
//рус
include_once(_APP_PATH. 'modules/admin/controllers/users/users_admin_http.php');
class users_admin_http_ajax extends users_admin_http
{
    public function check_loginPrAction ()
    {
        $obj = SysLoader::GetModResponse();
        if (! $this->User()->IsLogged()) {
            $obj->SetStatus(MR_STATUS_ERROR);
            $obj->SetMessage('Сессия закончилась. Войдите снова, используя форму выше.');
            $obj->result = $this->GetSmallLoginForm();
        } else {
            $obj->SetStatus(MR_STATUS_OK);
        }
        $this->Response()->SetBody($obj);
        $this->Libs()->session->DoNotUpdate();
    }
    
    public function login_processPrAction ()
    {
        $obj = SysLoader::GetModResponse();
        if ($this->User()->IsLogged()) {
            $obj->SetStatus(MR_STATUS_OK);
            $t = $this->GetTemplator();
            $t->user = $this->User();
            $obj->result = $t->render('users/_userstatus.phtml');
        } else {
            $obj->SetStatus(MR_STATUS_ERROR);
            $obj->SetMessage($this->User()->GetErrorMessage());
        }
        $this->Response()->SetBody($obj);
    }
    
    const MAX_USERS_IN_LIST = 50;
    
    public function users_listPrAction()
    {
    	$name = trim(urldecode($this->Request()->getPost('tag')));
    	$max = (int)$this->Request()->getPost('max');
		$users = $this->GetMapper()->GetUsersByName($name, min($max, self::MAX_USERS_IN_LIST));    	

		$preparedData = array();
		$i = 0;
		foreach ($users as $user)
    	{
			$preparedData[$i] = new stdClass(); 
			$preparedData[$i]->tag = $user->GetUserName();  
			$preparedData[$i]->id = $user->GetUserId();
			$i++; 		
    	}
		
        $this->Response()->SetBody($this->Libs()->json->encode($preparedData));
        $this->GetModule()->GetDecorator()->SetTemplate('SimpleJson'); 	
    }    
}